package com.senon.mvpretrofitrx.mvp.activity;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.senon.mvpretrofitrx.R;
import com.senon.mvpretrofitrx.mvp.api.Api;
import com.senon.mvpretrofitrx.mvp.base.BaseActivity;
import com.senon.mvpretrofitrx.mvp.base.BasePresenter;
import com.senon.mvpretrofitrx.mvp.base.BaseView;
import com.senon.mvpretrofitrx.mvp.presenter.NewsPresenter;
import com.senon.mvpretrofitrx.mvp.progress.ObserverResponseListener;
import com.senon.mvpretrofitrx.mvp.progress.ProgressObserver;
import com.senon.mvpretrofitrx.mvp.test.BaseResponse;
import com.senon.mvpretrofitrx.mvp.test.News;
import com.senon.mvpretrofitrx.mvp.test.NewsResult;
import com.senon.mvpretrofitrx.mvp.utils.ExceptionHandle;
import com.senon.mvpretrofitrx.mvp.utils.ToastUtil;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.HttpException;

public class MultiHttpActivity extends AppCompatActivity {

    Button btnMulti;
    private Disposable mDisposable;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multi_http);

        btnMulti = findViewById(R.id.btn_multi);
        btnMulti.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                init();
            }
        });
    }

    public void init() {
        HashMap<String, String> params = new HashMap<>();
        params.put("key", "d600a9f93f8ab5c302a632647f1788e4");
        params.put("type", "top");
        //newsPresenter.getNews(params, true, false);

        Api.getJuheService().getNews(params)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResponse<NewsResult>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mDisposable = d;
                    }

                    @Override
                    public void onNext(BaseResponse<NewsResult> newsResultBaseResponse) {
                        ToastUtil.showShortToast("请求成功");
                        NewsResult result = newsResultBaseResponse.getResult();
                        List<News> news = result.getData();
                        Log.i("TAG", "news:" + news.toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUtil.showExceptionLongToast(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
                /*.subscribe(new Observer<BaseResponse<NewsResult>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseResponse<NewsResult> newsResultBaseResponse) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });*/
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
    }
}
